﻿using BW.IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BW.DAL
{
    public class BaseDAL<T> : IBaseDAL<T> where T : class  //约束了泛型只能使用引用类型
    {
        public BaseDAL(MyContext context)
        {
            Context = context;
        }

        public MyContext Context { get; }

        public async Task<int> AddAsync(T t)
        {
            await Context.Set<T>().AddAsync(t);
            return await Context.SaveChangesAsync();
        }

        public async Task<int> AddRangeAsync(List<T> list)
        {
            await Context.Set<T>().AddRangeAsync(list);
            return await Context.SaveChangesAsync();
        }

        public async Task<int> DeleteAsync(T t)
        {
            Context.Remove(t);
            return await Context.SaveChangesAsync();
        }

        public async Task<int> DeleteRangeAsync(List<T> list)
        {
            Context.Remove(list);
            return await Context.SaveChangesAsync();
        }

        public IQueryable<T> GetAllAsync()
        {
            return Context.Set<T>();
        }

        public async Task<int> UpDateAsync(T t)
        {
            Context.Update<T>(t);
            return await Context.SaveChangesAsync();
        }

        public async Task<int> UpDateRangeAsync(List<T> list)
        {
            Context.UpdateRange(list);
            return await  Context.SaveChangesAsync();
        }
    }
}
